package com.qitancai.huazistudentmanagement.common.api;


public class Result<T> {

    /**
     * 2XX success
     * others failed
     **/
    private int code;
    private String message;
    private T data;

    protected Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(ApiCode.SUCCESS.getCode(), ApiCode.SUCCESS.getMessage(), data);
    }

    /**
     * 自定义操作成功的响应信息
     * @param message
     **/
    public static <T> Result<T> success(T data, String message) {
        return new Result<T>(ApiCode.SUCCESS.getCode(), message, data);
    }

    public static <T> Result<T> failed() {
        return new Result<T>(ApiCode.FAILED.getCode(), ApiCode.FAILED.getMessage(), null);
    }

    /**
     * 自定义操作失败的错误信息
     * @param message
     **/
    public static <T> Result<T> failed(String message) {
        return new Result<T>(ApiCode.FAILED.getCode(), message, null);
    }


    public static <T> Result<T> validateFailed() {
        return new Result<T>(ApiCode.VALIDATE_FAILED.getCode(), ApiCode.VALIDATE_FAILED.getMessage(), null);
    }

    /**
     * 自定义校验失败的错误信息
     * @param message
     **/
    public static <T> Result<T> validateFailed(String message) {
        return new Result<T>(ApiCode.VALIDATE_FAILED.getCode(), message, null);
    }

    public static <T> Result<T> unknownException() {
        return new Result<T>(ApiCode.UNKNOWN_EXCEPTION.getCode(), ApiCode.UNKNOWN_EXCEPTION.getMessage(), null);
    }

    /**
     * 自定义其他异常操作的错误信息
     * @param message
     **/
    public static <T> Result<T> unknownException(String message) {
        return new Result<T>(ApiCode.UNKNOWN_EXCEPTION.getCode(), message, null);
    }
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
